<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/7/6
 * Time: 16:56
 */

namespace pay\modules\backend\controllers;

use pay\modules\backend\models\Application;
use pay\libs\Msg;
use pay\libs\Tools;
use Yii;
use pay\modules\backend\models\Configuration;

class PlatformController extends Configuration
{

    /*创建应用,由认证中心调取*/
    public function actionOpenApp()
    {
        $appInfo = '';//开通成功后，存入此
        if(Yii::$app->request->isPost){
            // if(1){
            //     $this->post['appInfoId'] = '88';
            //     $this->post['app_name'] = '88';
            //     $this->post['comid'] = '00000000';
            //     $this->post['buyTime'] = '2';
            $app = new App;
            if(($appInfo = $app->addApp($this->post)) !== false){
                $this->GenerateApplica($this->post['appInfoId'],$this->post['app_name']);  // 开通实例
                $backInfo = [0, '开通APP实例成功'];//返回信息
            }else{
                if($app->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $app->getErrors();//异常信息
                }
            }

        }
        // P($appInfo);

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        $args = Tool::operationLogArgs($backInfo);
        $args['appid'] = isset($appInfo['appid'])?$appInfo['appid']:'';
        $args['comid'] = isset($this->post['comid'])?$this->post['comid']:'';
        if(gettype( $res = OperationLog::addLog($args, $exception, ['appinfo' => $appInfo]) ) != 'boolean'){
            return $res;
        };
    }


    /*** 开通实例
     * @param $app_id
     * @param $name
     * @return bool
     */
    private function GenerateApplica($app_id,$name)
    {
        $applicaModel = new Application();
        $msg = new Msg();
        if ($applicaModel->addLicat($app_id,$name)){
            return true;
        }else{
            $msg->insertMsg('10045',$app_id);  // 插入错误信息
            return false;
        }
    }
}